package com.jackrain.nea.fc.cp.mapper;

import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.Set;

/**
 * 职等006
 *
 * @author wuwenxue
 * @date 2017/12/15
 **/
@Mapper
public interface ViewHrJobRankMapper {

    /**
     * 查询CP_C_JOBRANK表中是否存在Hr记录
     * @param hr006 HR006_ID
     * @return int
     */
    @Select("SELECT COUNT(1) FROM CP_C_JOBRANK WHERE HR006_ID=#{hr006}")
    int selForHR006id(String hr006);

    /**
     * 修改表记录
     * @param jsonObject 修改数据
     * @return int
     */
    @UpdateProvider(type = ViewHrJobRankMapper.SaveJobRank.class, method = "update")
    int updateJobRank(JSONObject jsonObject);

    /**
     * 新增表记录
     * @param jsonObject 新增数据
     * @return int
     */
    @InsertProvider(type = ViewHrJobRankMapper.SaveJobRank.class, method = "insert")
    int insertJobRank(JSONObject jsonObject);

    class SaveJobRank {

        public String update(final JSONObject jsonObject) {
            return new SQL() {
                {
                    UPDATE("CP_C_JOBRANK");
                    for (String key : jsonObject.keySet()) {
                        if (!"ID".equals(key)) {
                            SET(key + "=#{" + key + "}");
                        }
                    }
                    WHERE("HR006_ID=#{HR006_ID}");
                }
            }.toString();
        }

        public String insert(final JSONObject jsonObject) {
            return new SQL() {
                {
                    Set<String> keySet = jsonObject.keySet();
                    INSERT_INTO("CP_C_JOBRANK");
                    for (int i = 0; i < jsonObject.size(); i++) {
                        String key = (String) keySet.toArray()[i];
                        VALUES(key, "#{" + key + "}");
                    }
                }
            }.toString();
        }

    }


}
